const app = getApp();
const request = require('../../utils/request.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    work_list: null
  },
  initData: function () {
    //获取当日打卡人员次数
    request({
      url: '/api/data',
      method: 'post',
      data: {
        "model_code": "emp_work_rollcall",
        "conds": [{
          "col": "date",
          "val": this.formatDateToStr(new Date(), "yyyy-MM-dd")
        }]
      },
    }).then(res => {
      res.data && res.data.forEach(item => {
        if (item.num % 2 == 0) {
          item.class = "person-offwork";
        } else {
          item.class = "person-onwork";
        }
      });
      this.setData({
        work_list: res.data
      });
    });
  },
  personClick: function (event) {
    let emp_code = event.currentTarget.dataset.emp_code;
    request({
      url: '/api/save/add',
      method: 'post',
      data: {
        "table_name": "man_work_clock",
        conds: [{
            "col": "datetime",
            "val": this.formatDateToStr(new Date(), 'yyyy-MM-dd hh:mm:ss')
          },
          {
            "col": "emp_code",
            "val": emp_code
          }
        ]
      }
    }).then(res => {
      this.initData();
    });
  },
  formatDateToStr: function (time, pattern) {
    var year = time.getFullYear();
    var month = time.getMonth() + 1;
    var day = time.getDate();
    var hour = time.getHours();
    var minute = time.getMinutes();
    var second = time.getSeconds();
    if (pattern === 'yyyy-MM-dd hh:mm:ss') {
      return year + '-' + (month < 10 ? '0' + month : month) + '-' + (day < 10 ? '0' + day : day) + ' ' + (hour < 10 ? '0' + hour : hour) + ':' + (minute < 10 ? '0' + minute : minute) + ':' + (second < 10 ? '0' + second : second);
    } else if (pattern === 'yyyy-MM-dd') {
      return year + '-' + (month < 10 ? '0' + month : month) + '-' + (day < 10 ? '0' + day : day);
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    this.initData();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.initData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})